package com.jiai.cloud.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TGoodType;
import com.jiai.cloud.sys.service.ITGoodTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 类型表 前端控制器
 * </p>
 *
 * @author 佳爱科技
 * @since 2017-05-24
 */
@RestController
@RequestMapping("/api/v1/goodtype")
public class TGoodTypeController {
    @Autowired
    private ITGoodTypeService itGoodTypeService;
    //增
    @RequestMapping(value="/addGoodType", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public Object addGoodType(TGoodType goodType)throws  ServiceException{

        if(StringUtils.isEmpty(goodType.getTypeName())){
            throw new  ServiceException("typeName  is not null");
        }
        int  total=itGoodTypeService.selectCount(new EntityWrapper().eq("typeName",goodType.getTypeName()));
        if(total>0){
            throw new ServiceException("同名存在，添加错误");
        }
        itGoodTypeService.insert(goodType);
        return  RestResultGenerator.genSuccessResult(goodType);
    }

    //ID删
    @RequestMapping(value="/delete/{id}", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public Object delete(@PathVariable("id") Integer id) throws ServiceException {
        try {

            return RestResultGenerator.genSuccessResult(itGoodTypeService.deleteById(id));
        }catch (Exception e){
            throw new ServiceException("删除失敗",e.getMessage());
        }
    }


    //查所有
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll() throws ServiceException{
        try {
            return RestResultGenerator.genSuccessResult(itGoodTypeService.selectList(null));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //根据ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itGoodTypeService.selectById(id));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    @ResponseBody
    public Object edit(@RequestBody TGoodType tGoodType)throws ServiceException {
        try {
            itGoodTypeService.insertOrUpdate(tGoodType);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("编辑失敗", e.getMessage());
        }
    }
}
